//
//  GrapicView.m
//  Demo
//
//  Created by LG on 2018/12/5.
//  Copyright © 2018 LG. All rights reserved.
//

#import "GrapicView.h"

@implementation GrapicView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self drawView];
    }
    return self;
}

- (void)drawView
{
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(100, 100), NO, 0);
    UIBezierPath *beizerPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 100, 100)];
    beizerPath.lineWidth = 3.;
    [[UIColor redColor]setStroke];
    [beizerPath stroke];
    UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageView* iv = [[UIImageView alloc] initWithImage:im];
    [self addSubview: iv];
    iv.center = self.center;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
//    CGContextRef con = UIGraphicsGetCurrentContext();
//    // 绘制一个黑色的垂直黑色线，作为箭头的杆子
//    CGContextMoveToPoint(con, 100, 100);
//    CGContextAddLineToPoint(con, 100, 19);
//    CGContextSetLineWidth(con, 20);
//    CGContextStrokePath(con);
//    // 绘制一个红色三角形箭头
//    CGContextSetFillColorWithColor(con, [[UIColor redColor] CGColor]);
//    CGContextMoveToPoint(con, 80, 25);
//    CGContextAddLineToPoint(con, 100, 0);
//    CGContextAddLineToPoint(con, 120, 25);
//    CGContextFillPath(con);
//    // 从箭头杆子上裁掉一个三角形，使用清除混合模式
//    CGContextMoveToPoint(con, 90, 101);
//    CGContextAddLineToPoint(con, 100, 90);
//    CGContextAddLineToPoint(con, 110, 101);
//    CGContextSetBlendMode(con, kCGBlendModeClear);
//    CGContextFillPath(con);
    
//    UIBezierPath* p = [UIBezierPath bezierPath];
//    [p moveToPoint:CGPointMake(100,100)];
//    [p addLineToPoint:CGPointMake(100, 19)];
//    [p setLineWidth:20];
//    [p stroke];
//    [[UIColor redColor] set];
//    [p removeAllPoints];
//    [p moveToPoint:CGPointMake(80,25)];
//    [p addLineToPoint:CGPointMake(100, 0)];
//    [p addLineToPoint:CGPointMake(120, 25)];
//    [p fill];
//    [p removeAllPoints];
//    [p moveToPoint:CGPointMake(90,101)];
//    [p addLineToPoint:CGPointMake(100, 90)];
//    [p addLineToPoint:CGPointMake(110, 101)];
//    [p fillWithBlendMode:kCGBlendModeClear alpha:.5];
    
    CGContextRef con = UIGraphicsGetCurrentContext();
    // 在上下文裁剪区域中挖一个三角形状的孔
    CGContextMoveToPoint(con, 90, 100);
    CGContextAddLineToPoint(con, 100, 90);
    CGContextAddLineToPoint(con, 110, 100);
    CGContextClosePath(con);
    CGContextAddRect(con, CGContextGetClipBoundingBox(con));
    // 使用奇偶规则，裁剪区域为矩形减去三角形区域
    CGContextEOClip(con);
    // 绘制垂线
    CGContextMoveToPoint(con, 100, 100);
    CGContextAddLineToPoint(con, 100, 19);
    CGContextSetLineWidth(con, 20);
    CGContextStrokePath(con);
    // 画红色箭头
    CGContextSetFillColorWithColor(con, [[UIColor redColor] CGColor]);
    CGContextMoveToPoint(con, 80, 25);
    CGContextAddLineToPoint(con, 100, 0);
    CGContextAddLineToPoint(con, 120, 25);
    CGContextFillPath(con);
}


@end
